﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace _2011DBProject
{
    /// <summary>
    /// class for managing Web.config file as well as Session Variables
    /// </summary>
    public static class Settings
    {
        #region Fields

        private static string _strConnectionString = "";
        private static string _strAccountID = "";
        private static decimal _strServer = 1;
        private static decimal _decCh = 1;
        private static string _strCharacterName = "";
        private static decimal _decGameroom_No = 1;
        private static string _guildName = "";

        #endregion

        #region Properties

        public static string strConnectionString
        {
            get { return _strConnectionString; }
            set { _strConnectionString = value; }
        }

        public static string strAccountID
        {
            get { return Settings._strAccountID; }
            set { Settings._strAccountID = value; }
        }

        public static decimal decServer
        {
            get { return Settings._strServer; }
            set { Settings._strServer = value; }
        }

        public static decimal decCh
        {
            get { return Settings._decCh; }
            set { Settings._decCh = value; }
        }

        public static string strCharacterName
        {
            get { return Settings._strCharacterName; }
            set { Settings._strCharacterName = value; }
        }

        public static decimal decGameroom_No
        {
            get { return Settings._decGameroom_No; }
            set { Settings._decGameroom_No = value; }
        }

        public static string strGuildName
        {
            get { return Settings._guildName; }
            set { Settings._guildName = value; }
        }

        #endregion

        static Settings()
        {
            try
            {
                //Get the default connection string configuration named "ConnectionString"
                strConnectionString = GetConnectionString("");
                RefreshSessionVariables(true, false);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        #region Methods

        /// <summary>
        /// Refetch Session variables to reflect any updated data
        /// </summary>
        public static void RefreshSessionVariables(bool initialLoad, bool server)
        {
            try
            {
                if (!initialLoad)
                {
                    strAccountID = HttpContext.Current.Session["USER"].ToString(); //Get AccountID
                    if (server)
                    {
                        decServer = Convert.ToDecimal(HttpContext.Current.Session["SERVER"]); //Get SERVER No
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void RefreshCh()
        {
            decCh = Convert.ToDecimal(HttpContext.Current.Session["Ch"]); //Get Ch No
        }


        #region GetConnectionstring
        /// <summary>
        /// method to retrieve connection stringed in the web.config file
        /// </summary>
        /// <param name="str">Name of the connection</param>
        /// <remarks>Need a reference to the System.Configuration Namespace</remarks>
        /// <returns></returns>
        private static string GetConnectionString(string str)
        {
            //variable to hold our return value
            string conn = string.Empty;
            //check if a value was provided
            if (!string.IsNullOrEmpty(str))
            {
                //name provided so search for that connection
                conn = ConfigurationManager.ConnectionStrings[str].ConnectionString;
            }
            else
            //name not provided, get the 'default' connection
            {
                conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            }
            //return the value
            return conn;
        }
        #endregion

        #endregion
    }
}
